package com.example.demo2;

import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.context.ServletContextAware;

import javax.servlet.ServletContext;
import java.text.SimpleDateFormat;
import java.util.Date;

@Configuration
@EnableScheduling
public class TimerService implements ServletContextAware {
    //        每分钟启动
    @Scheduled(cron = "0/5 * * * * ?")
//    @Scheduled(initialDelay = 1000,fixedRate = 2000)
    public void timerToNow() {
        int count = (int) servletContext.getAttribute("limit");
        System.out.println("  count limit:" + count);
        if (count <= 9990) {
            System.out.println("  小于啦");
            servletContext.setAttribute("limit", 10000);
        }
        System.out.println("now time:" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
    }

    //    @Scheduled(initialDelay = 1000,fixedRate = 2000)
//@Scheduled(cron = "0/5 * * * * ?")
//public void timerToNow2() {
//        System.out.println("2 now time:" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
//    }
    private ServletContext servletContext;

    @Override
    public void setServletContext(ServletContext servletContext) {
        this.servletContext = servletContext;
    }
}
